/*
 * @Author: biankai
 * @Date: 2021-11-04 14:27:50
 * @LastEditTime: 2021-11-10 11:35:48
 * @LastEditors: biankai
 * @FilePath: /gitee/devops_service/src/services/jenkins/job.service.ts
 */
import {
    Injectable,
    Inject,
    HttpService,
    BadRequestException,
} from '@nestjs/common';
import * as fs from 'fs'
import * as path from 'path'
import { BaseService } from './base.service';
@Injectable()
export class JobService extends BaseService {

    constructor(@Inject(HttpService) private readonly httpService: HttpService) {
        super()
    }

    async jobList() {
        const resBody = await this.jenkinsObj.job.list()
        return await resBody;
    }
    async jobGet(jobName) {
        const resBody = await this.jenkinsObj.job.get(jobName,)
        return await resBody;
    }

    async jobCreate(jobName, configXml) {
        const options = { folder: '../../../config' };
        const filePath = path.resolve(__dirname, options.folder, 'job_config.xml');
        var xml = fs.readFileSync(filePath, 'utf8')

        var res = await this.jenkinsObj.job.create(jobName, xml,)
        return res

    }

    async jobGetConfig(jobName) {
        const resBody = await this.jenkinsObj.job.config(jobName)
        return await resBody;
    }

    async jobSetConfig(jobName, configXml) {
        const resBody = await this.jenkinsObj.job.config(jobName, configXml)
        return await resBody;
    }

    async jobDestroy(jobName) {
        const resBody = await this.jenkinsObj.job.destroy(jobName,)
        return await resBody;
    }

    async jobBuild(jobName) {
        const resBody = await this.jenkinsObj.job.build(jobName,)
        return await resBody;
    }

    async jobDisable(jobName) {
        const resBody = await this.jenkinsObj.job.disable(jobName,)
        return await resBody;
    }

    async jobEnable(jobName) {
        const resBody = await this.jenkinsObj.job.enable(jobName,)
        return await resBody;
    }

    async jobExists(jobName) {
        const resBody = await this.jenkinsObj.job.exists(jobName,)
        return await resBody;
    }

    async jobCopy(jobName, fromJob) {
        const resBody = await this.jenkinsObj.job.copy(jobName, fromJob,)
        return await resBody;
    }
}
